jQuery(document).ready(function(){
    randomStyle();
    checkInViewSection();
    submitContactForm();
    doTextAppear();
    menuHeight = 93;
    doRestyle('#top-section', menuHeight, autoLoad = 1);
    

    jQuery('nav li a').click(function(e){
        e.preventDefault();
        jQuery('nav li a.active').removeClass('active');
        jQuery(this).addClass('active');
        divSelection = (jQuery(this).attr('data-tracker'));
        doRestyle(divSelection, menuHeight, autoLoad = 0);
        return false;
    });

});

$(function () {
    var filterList = {

        init: function () {
            // MixItUp plugin
            // http://mixitup.io
            $('#portfoliolist').mixitup({
                targetSelector: '.portfolio',
                filterSelector: '.filter',
                effects: ['fade'],
                easing: 'snap',
                // call the hover effect
                onMixEnd: filterList.hoverEffect()
            });

        },

        hoverEffect: function () {

            // Simple parallax effect
            $('#portfoliolist .portfolio').hover(
                function () {
                    $(this).find('.label').stop().animate({bottom: 0}, 200, 'easeOutQuad');
                    $(this).find('img').stop().animate({top: -30}, 500, 'easeOutQuad');
                },
                function () {
                    $(this).find('.label').stop().animate({bottom: -40}, 200, 'easeInQuad');
                    $(this).find('img').stop().animate({top: 0}, 300, 'easeOutQuad');
                }
            );

        }
    };

    // Run the show!
    filterList.init();

});

function submitContactForm() {
        // Contact Form
    $("#contact").submit(function(e){
        e.preventDefault();
        var name = $("#name").val();
        var email = $("#email").val();
        var text = $("#text").val();
        var dataString = 'name=' + name + '&email=' + email + '&text=' + text;
        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };

        if (isValidEmail(email) && (text.length > 1) && (name.length > 1)){
            $.ajax({
                type: "POST",
                url: "application/contactprocess.php",
                data: dataString,
                dataType: "json",
                success: function(response){
                    if(response == true) {
                        $('.mail-status.error').hide();
                        $('.mail-status.success').show(0).delay(10000).hide(0);
                        return true;
                    }
                    $('.mail-status.success').hide();
                    $('.mail-status.error').show(0).delay(10000).hide(0);
                }
            });
        } else {
            $('.success').hide();
            $('.error').show();
        }
        return false;
    });
}

function checkInViewSection() {
    $('[data-locator="top-section"],[data-locator="services-wrap"],[data-locator="portfolio-wrap"],[data-locator="contact-wrap"],[data-locator="paypal"]').bind('inview', function (event, visible) {
        if (visible == true) {
            menuSelection = '#' + $(this).attr('data-locator');
            jQuery('nav li a.active').removeClass('active');
            $("[data-tracker='"+menuSelection+"']").addClass('active')
        }
    });
}

function doTextAppear() {
//       jQuery('.animated-1, .animated-2').delay(1000).fadeOut().fadeIn();
}

function randomStyle() {
    maxNumBg = 2;
    randomNum = Math.floor((Math.random()*maxNumBg)+1);
    if (randomNum > 1) {
        jQuery('body').removeClass('bg-v1');
        jQuery('body').addClass('bg-v' + randomNum);
        jQuery('.main-nav').removeClass('nav-v1');
        jQuery('.main-nav').addClass('nav-v' + randomNum);
    }
}

function doRestyle(div, menuHeight, loader) {
    addableMargin = 0;
    viewportHeight = jQuery(window).height();
    if(loader) {
        //Fix for missing 17px on viewport on document.ready
        viewportHeight = viewportHeight + 17;
    }
    // Remove height of menu-bar
    browserHeight = viewportHeight - menuHeight;
    sectionHeight = jQuery(div).height();

    if (browserHeight > sectionHeight) {
        extraMargin = browserHeight - sectionHeight;
        addableMargin = extraMargin / 2;
        midViewport = browserHeight / 2;
        jQuery(div).css({
            "margin-top": menuHeight + addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
    }
    
    doScroll(div, addableMargin, menuHeight);
    return true;
}

function doScroll (div, addableMargin, menuHeight) {

    scrollHeight = jQuery(div).offset().top;
    
    requiredScroll = scrollHeight - menuHeight - addableMargin;
    jQuery('html, body').animate({
        
        scrollTop: requiredScroll
    }, 'slow');
    return true;
}
